package model.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLXML;

/**
 * Abstract class
 * @author Potseluev S.V.
 *
 */
public abstract class BaseCallStatement {
	
	protected Connection connection					= null;			//Connection to database
	protected CallableStatement callableStatement 	= null;			//CallableStatement
	String leftPartSql 								= "{call ";		//Left part of sql call
	String rightPartSql								= "(?, ?)}";	//right part of sql call
																	//result will {call procName(?, ?)} As a parameter procedure will get xml and result will xml too

	protected int returnedDataNumber 	= 1;
	protected int xmlDataNumber			= 2;
	
	public BaseCallStatement() throws Exception {
		connection 	= ConnectionPool.getConnection(); //Get free connection from connection pool
		connection.setAutoCommit(false);
	}
	
	
	/**
	 * Execute store procedure
	 * @throws Exception
	 */
	public void execute() throws Exception {
		
		callableStatement.execute();	// execute stored procedure
	 
	}
	
	/**
	 * Commit transaction
	 * @throws Exception
	 */
	public void commitTransaction() throws Exception {
		connection.commit();
	}
	
	/**
	 * Get CallableStatement for manual control
	 * @return
	 */
	public CallableStatement getCallableStatement() {
		return callableStatement;
	}
	
	/**
	 * Close statement
	 * @throws Throwable
	 */
	public void close() throws Exception {
		callableStatement.close();
	}
	
	public void closeConnection() throws Exception {
		connection.close();
	}
	
	
}
